/*  SSTablePrintViewRow.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Tue Jul 08 2003.
//
//  the SSTablePrintViewRow groups and draws a set of NSCells. It should be used only in conjunction with the SSTablePrintView class


#import "SSTablePrintViewRow.h"


@implementation SSTablePrintViewRow

- (id)initWithColumns:(NSArray *)cols columnWidths:(NSArray *)widths origin:(NSPoint)orig
{
   //cols must be populated with NSCells or an NSCells subclass
   //widths must be populated with NSNumbers
   //both arrays must also contain the same number of objects
   
   if (self = [super init]) {
      [self setColumnArray:cols];
      [self setColumnWidthArray:widths];
      [self setFrameWithOrigin:orig];
   }
   
   return self;   
}

- (void)drawInView:(NSView *)view
{
   [[NSColor blackColor] set];
   [NSBezierPath fillRect:[self frame]];
   
   int i = 0;
   float minX = 1;

   while (i < [columnArray count]) {
      [[columnArray objectAtIndex:i] drawWithFrame:NSMakeRect(minX, frame.origin.y, [[columnWidthArray objectAtIndex:i] floatValue], (frame.size.height - 1)) inView:view];
      minX += ([[columnWidthArray objectAtIndex:i] floatValue] + 1);
      i++;
   }
}

- (NSRect)frame
{
   return frame;
}

- (BOOL)containsPoint:(NSPoint)point
{
   NSPoint origin = [self frame].origin;

   if(point.x > origin.x) {
      if(point.x < (origin.x + NSWidth([self frame]))) { //it is within our x range
         if(point.y > origin.y) {
            if(point.y < (origin.y + NSHeight([self frame]))) {
               //we contain this point
               return YES;
            }
         }
      }
   }
   return NO;
}

- (void)setColumnArray:(NSArray *)cols
{
   [cols retain];
   [columnArray release];
   columnArray = cols;
}

- (void)setColumnWidthArray:(NSArray *)widths
{
   [widths retain];
   [columnWidthArray release];
   columnWidthArray = widths;
}

- (void)setFrameWithOrigin:(NSPoint)orig
{
   float width = 0;
   float height = 0;
   int i = 0;
   NSSize cellSize;

   while (i < [columnWidthArray count]) {
      width += ([[columnWidthArray objectAtIndex:i] floatValue] + 1);
      
      i++;
   }

   i = 0;

   while (i < [columnArray count]) {
      cellSize = [(NSCell *)[columnArray objectAtIndex:i] cellSizeForBounds:NSMakeRect(0, 0, (float)[[columnWidthArray objectAtIndex:i] floatValue], 10000)];
      if(cellSize.height > height) {
         //if the min height for this cell is more than height use it's min height
         height = cellSize.height;
      }
      
      i++;
   }
   
   frame.origin = orig;
   frame.size.width = width;
   frame.size.height = (height + 1);
}

- (void)dealloc
{
   [columnArray release];
   [columnWidthArray release];
   [super dealloc];
}

@end
